package net.minecraft.src;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.ImageObserver;
import net.minecraft.src.TextureFX;

public class ModTextureAnimation extends TextureFX {

   private final int tickRate;
   private final byte[][] images;
   private int index;
   private int ticks;


   public ModTextureAnimation(int var1, int var2, BufferedImage var3, int var4) {
      this(var1, 1, var2, var3, var4);
   }

   public ModTextureAnimation(int var1, int var2, int var3, BufferedImage var4, int var5) {
      super(var1);
      this.index = 0;
      this.ticks = 0;
      this.tileSize = var2;
      this.tileImage = var3;
      this.tickRate = var5;
      this.ticks = var5;
      int var6 = var4.getWidth();
      int var7 = var4.getHeight();
      int var8 = (int)Math.floor((double)(var7 / var6));
      if(var8 <= 0) {
         throw new IllegalArgumentException("source has no complete images");
      } else {
         int var9 = (int)Math.sqrt((double)(this.imageData.length / 4));
         this.images = new byte[var8][];
         if(var6 != var9) {
            BufferedImage var10 = new BufferedImage(var9, var9 * var8, 6);
            Graphics2D var11 = var10.createGraphics();
            var11.drawImage(var4, 0, 0, var9, var9 * var8, 0, 0, var6, var7, (ImageObserver)null);
            var11.dispose();
            var4 = var10;
         }

         for(int var18 = 0; var18 < var8; ++var18) {
            int[] var17 = new int[var9 * var9];
            var4.getRGB(0, var9 * var18, var9, var9, var17, 0, var9);
            this.images[var18] = new byte[var9 * var9 * 4];

            for(int var12 = 0; var12 < var17.length; ++var12) {
               int var13 = var17[var12] >> 24 & 255;
               int var14 = var17[var12] >> 16 & 255;
               int var15 = var17[var12] >> 8 & 255;
               int var16 = var17[var12] >> 0 & 255;
               this.images[var18][var12 * 4 + 0] = (byte)var14;
               this.images[var18][var12 * 4 + 1] = (byte)var15;
               this.images[var18][var12 * 4 + 2] = (byte)var16;
               this.images[var18][var12 * 4 + 3] = (byte)var13;
            }
         }

      }
   }

   public void onTick() {
      if(this.ticks >= this.tickRate) {
         ++this.index;
         if(this.index >= this.images.length) {
            this.index = 0;
         }

         this.imageData = this.images[this.index];
         this.ticks = 0;
      }

      ++this.ticks;
   }
}
